package rdw.http;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;

import rdw.robot.RobotCallback;

/**
 * HttpGetRequest handles GET requests for the HttpServer.
 */
public class HttpGetRequest
{
	private DataOutputStream out;
	private RobotCallback callback;
	
	public HttpGetRequest(DataOutputStream out, RobotCallback callback)
	{
		this.out = out;
		this.callback = callback;
	}
	
	/**
	 * handleRequest handles the GET request string.
	 */
	public void handleRequest(String get) throws IOException
	{
		StringTokenizer tokens = new StringTokenizer(get);
		tokens.nextToken();
		String getUrl = tokens.nextToken();
		
		if (getUrl.equals("/"))
		{
			System.out.println("sending main.html");
			sendTextFile("/webcontent/main.html", "text/html");	
		}
		else if (getUrl.equals("/menu.js"))
		{
			System.out.println("sending menu.js");
			sendTextFile("/webcontent/menu.js", "application/javascript");
		}
		else if (getUrl.equals("/menu_prototype.js"))
		{
			System.out.println("sending menu.js");
			sendTextFile("/webcontent/menu_prototype.js", "application/javascript");
		}
		else if (getUrl.equals("/menu.css"))
		{
			System.out.println("sending menu.css");
			sendTextFile("/webcontent/menu.css", "text/css");
		}
		else if (getUrl.startsWith("/screenshot"))
		{
			System.out.println("sending screenshot");
			out.write("HTTP/1.0 200 OK\r\n".getBytes());
			out.write("Content-Type: image/jpeg\r\n".getBytes());
			out.write("\r\n".getBytes());
			
			BufferedImage image = callback.getScreenshot();
			ImageIO.write(image, "jpg", out);
		}
		else if (getUrl.startsWith("/click"))
		{
			// /click?button=left|right&click=single|double&x=i&y=i
			UrlParameterDecoder params = new UrlParameterDecoder(getUrl.substring(getUrl.indexOf("?")+1));
			String button = params.getArgument("button");
			String click = params.getArgument("click");
			String xPos = params.getArgument("x");
			String yPos = params.getArgument("y");

			callback.receiveClick(button, click, new Integer(xPos).intValue(), new Integer(yPos).intValue());
		}
		else if (getUrl.startsWith("/text?"))
		{
			String text = getUrl.substring(getUrl.indexOf('?')+1);
			callback.receiveText(text);
		}
		else if (getUrl.equals("/shutdown"))
		{
			System.exit(0);
		}
		else if (getUrl.equals("/scrollup"))
		{
			callback.receiveScrollUp();
		}
		else if (getUrl.equals("/scrolldown"))
		{
			callback.receiveScrollDown();
		}
	}
	
	/**
	 * sendTextFile reads and dumps the provided filename to the outputstream HTTP.
	 */
	private void sendTextFile(String filename, String contentType) throws IOException
	{
		InputStreamReader in = new InputStreamReader(getClass().getResourceAsStream(filename));
		BufferedReader reader = new BufferedReader(in);

		out.write("HTTP/1.0 200 OK\r\n".getBytes());
		
		String contentTypeLine = "Content-Type: " + contentType + "\r\n";
		out.write(contentTypeLine.getBytes());
		out.write("\r\n".getBytes());
		
		String line = null;
		while ((line = reader.readLine()) != null)
		{
			out.write(line.getBytes());
			out.write("\r\n".getBytes());
		}
		reader.close();
		reader = null;
		
		in.close();
		in = null;
	}
}
